package com.matt.foundation.exception;

import com.matt.commons.exception.BusinessException;
import com.matt.foundation.model.StatusResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;


/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/6/2
 * *******************************
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody //在返回自定义相应类的情况下必须有，这是@ControllerAdvice注解的规定
    public StatusResponse exceptionHandler(RuntimeException e, HttpServletResponse response) {
        e.printStackTrace();

        if (e instanceof BusinessException)
            return StatusResponse.error(((BusinessException) e).getErrorCode(), e.getMessage());

        return StatusResponse.undefined();
    }
}
